home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / scandir_win32.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  3.0 KB  |  103 lines

  1. //
  2. // "$Id: scandir_win32.c,v 1.11 1999/01/07 19:17:47 mike Exp $"
  3. //
  4. // WIN32 scandir function for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // Emulation of posix scandir() call
  27.  
  28. #include <config.h>
  29. #include <string.h>
  30. #include <windows.h>
  31. #include <stdlib.h>
  32.  
  33. struct dirent { char d_name[1]; };
  34.  
  35. int scandir(const char *dirname, struct dirent ***namelist,
  36.     int (*select)(struct dirent *),
  37.     int (*compar)(struct dirent **, struct dirent **)) {
  38.   int len;
  39.   char *findIn, *d;
  40.   WIN32_FIND_DATA find;
  41.   HANDLE h;
  42.   int nDir = 0, NDir = 0;
  43.   struct dirent **dir = 0, *selectDir;
  44.   unsigned long ret;
  45.  
  46.   len    = strlen(dirname);
  47.   findIn = malloc(len+5);
  48.   strcpy(findIn, dirname);
  49.   for (d = findIn; *d; d++) if (*d=='/') *d='\\';
  50.   if ((len==0)) { strcpy(findIn, ".\\*"); }
  51.   if ((len==1)&& (d[-1]=='.')) { strcpy(findIn, ".\\*"); }
  52.   if ((len>0) && (d[-1]=='\\')) { *d++ = '*'; *d = 0; }
  53.   if ((len>1) && (d[-1]=='.') && (d[-2]=='\\')) { d[-1] = '*'; }
  54.   
  55.   if ((h=FindFirstFile(findIn, &find))==INVALID_HANDLE_VALUE) {
  56.     ret = GetLastError();
  57.     if (ret != ERROR_NO_MORE_FILES) {
  58.       // TODO: return some error code
  59.     }
  60.     *namelist = dir;
  61.     return nDir;
  62.   }
  63.   do {
  64.     selectDir=(struct dirent*)malloc(sizeof(struct dirent)+strlen(find.cFileName));
  65.     strcpy(selectDir->d_name, find.cFileName);
  66.     if (!select || (*select)(selectDir)) {
  67.       if (nDir==NDir) {
  68.     struct dirent **tempDir = calloc(sizeof(struct dirent*), NDir+33);
  69.     if (NDir) memcpy(tempDir, dir, sizeof(struct dirent*)*NDir);
  70.     if (dir) free(dir);
  71.     dir = tempDir;
  72.     NDir += 32;
  73.       }
  74.       dir[nDir] = selectDir;
  75.       nDir++;
  76.       dir[nDir] = 0;
  77.     } else {
  78.       free(selectDir);
  79.     }
  80.   } while (FindNextFile(h, &find));
  81.   ret = GetLastError();
  82.   if (ret != ERROR_NO_MORE_FILES) {
  83.     // TODO: return some error code
  84.   }
  85.   FindClose(h);
  86.  
  87.   free (findIn);
  88.  
  89.   if (compar) qsort (dir, nDir, sizeof(*dir),
  90.              (int(*)(const void*, const void*))compar);
  91.  
  92.   *namelist = dir;
  93.   return nDir;
  94. }
  95.  
  96. int alphasort (struct dirent **a, struct dirent **b) {
  97.   return strcmp ((*a)->d_name, (*b)->d_name);
  98. }
  99.  
  100. //
  101. // End of "$Id: scandir_win32.c,v 1.11 1999/01/07 19:17:47 mike Exp $".
  102. //
  103.